<?php 
// +----------------------------------------------------------------------
// | Item        短信记录模型
// +----------------------------------------------------------------------
// | Copyright
// +----------------------------------------------------------------------
// | Versions
// +----------------------------------------------------------------------
// | Author       YanHao
// +----------------------------------------------------------------------
// | Date         2017-07-12
// +----------------------------------------------------------------------

namespace app\admin\model;

class SnsLog extends Base 
{

    /**
     * 查询短信记录
     * @Author  YanHao
     * @Date    2017-07-13
     * @version [version]
     * @param   array      $where    [description]
     * @param   [type]     $pageSize [description]
     * @param   [type]     $config   [description]
     * @return  [type]               [description]
     */
    public function getAllMsg(array $where, $pageSize, $config)
    {
        return $this->where($where)->field(true)->order('create_at')->paginate($pageSize, false, $config);
    } 
    /**
     * 再次发送短信
     * @Author  YanHao
     * @Date    2017-07-12
     * @version [version]
     * @param   [type]     $id [description]
     * @return  [type]         [description]
     */
    public function resendMsg($id)
    {
        $info = parent::getOne($id, true);
        if ($info) {
            if ($info['template_scene'] != 1) {
                // 对接短信类 将content 里的内容发送给 phone
                $res = (new phoneCLass)->sendMsg($info['phone'],$info['content'], true); 
                if ($res['code']) {
                    self::_sendUpdate($info['retry'], $id);
                }
            } else {    
                $res = ['code' => 0, 'msg' => '无需再次发送'];
            }
        } else {
            $res = ['code' => 0, 'msg' => '参数错误'];
        }
        return $res;
    }
    /**
     * 重发之后进行更新
     * @Author  YanHao
     * @Date    2017-07-12
     * @version [version]
     * @param   [type]     $retry 次数
     * @param   [type]     $id    id
     * @return  [type]            [description]
     */
    private function _sendUpdate($retry,$id)
    {
        $udpateData = [
            'upadate_at' => date('Y-m-d H:i:s'),
            'retry'      => ++$retry
        ];
        return $this->isUpdate(true)->save($udpateData,['id' => $id]);
    }
}